package com.authority.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.common.MD5Util;
import com.authority.entry.AuthOrganDO;
import com.authority.entry.AuthUserDO;
import com.authority.service.AuthOrganService;
import com.authority.service.UserService;
import com.authority.vo.SingleResultDO;


/** 
 * @Description: 密码重置
 * @author chenlushun
 * @date 2015年10月20日 下午2:15:54 
 *  
 */  

@Controller
@RequestMapping(value = "/user/UserLogin")
public class UserLogin extends BaseManager{
	
	private static final Logger log = LoggerFactory.getLogger(UserManager.class);

	@Resource
	private UserService userService;
	
	@Resource
	private AuthOrganService authOrganService;

	
	/**
	 * 重置密码
	 * @param account
	 * @param tel
	 * @param context
	 * @return
	 */
	@RequestMapping(value = "/resetPassword.json")
	@ResponseBody
	public SingleResultDO<AuthUserDO> doResetPassword(Long organId,String workNumber,String account,String tel){
		SingleResultDO<AuthUserDO> result=new SingleResultDO<AuthUserDO>();
		result.setSuccess(false);
		if(organId!=null&workNumber!=null){
			AuthOrganDO organ=authOrganService.queryAuthOrganById(organId);
			account=organ.getPrefix()+workNumber;
		}
		AuthUserDO user=userService.queryUserByAccount(account);
		if(user==null){
			result.setErrorDesc("账号不存在");
			return result;
		}
		if(user.getStatus()==0){
			result.setErrorDesc("账号已被禁用 ");
			return result;
		}
		if(!user.getTel().equals(tel)){
			result.setErrorDesc("输入的手机号码与账号信息不符");
			return result;
		}
		/*int randomPassword=(int)(Math.random()*1000000);
		//发短信给用户
		sendMessage(tel,randomPassword,user.getUserName());*/
		String encPassword=MD5Util.doubleMD5(String.valueOf(123456));
		AuthUserDO db=new AuthUserDO();
		db.setAccount(account);
		db.setTel(tel);
		db.setPassword(encPassword);
		log.info(encPassword);
		userService.updatePassword(db);
		result.setSuccess(true);
		return result;
	}
	
	
	/**
	 * @description 用户验证
	 * @param  
	 * @return boolean
	 */
	@RequestMapping(value = "/passwordCheck.json")
	@ResponseBody
	public boolean doPasswordCheck(String password){
		AuthUserDO user=userService.queryUserByAccount(getAccount());
		String passwd=user.getPassword();
		if(!passwd.equals(MD5Util.doubleMD5(password))){
			return false;
		}
		return true;
	}
	
	/**
	 * @description 密码修改
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/modifyPassword.json")
	@ResponseBody
	public Object doModifyPassword(String password){
		AuthUserDO user=userService.queryUserByAccount(getAccount());
		user.setPassword(MD5Util.doubleMD5(password));
		try {
			userService.updatePassword(user);
			return success();
		} catch (Exception e) {
			return fail(e);
		}
	}
}
